<%@page
	import="com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*" import="wildcard.database.DBAccess"
	import="wildcard.servlet.LoginBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>メイン</title>
</head>

<body>


	<%
		LoginBean loginBean = null;
		String dbId = null;
		String dbPassword = null;

		String[] smoke = { "喫煙", "禁煙" };

		// ユーザIDの取得
		if (session.getAttribute("AndroidUser") != null) {
			loginBean = (LoginBean) session.getAttribute("AndroidUser");
			dbId = loginBean.getLoginId().substring(0, 2);
			dbPassword = loginBean.getPassword();
		} else {
			loginBean = new LoginBean("AN00", "wildcard");
			session.setAttribute("AndroidUser", loginBean);
		}

		// テーブル情報の取得
		ArrayList<String> parameters = new ArrayList<String>();
		parameters.add("SQL2");
		ArrayList<ArrayList<String>> resultList = DBAccess.executeSelect(
				"AN", "wildcard", DBAccess.SQL_SELECT_COMMAND, parameters);

		String sql = resultList.get(1).get(0);
		parameters.clear();

		resultList = DBAccess.executeSelect("AN", "wildcard", sql,
				parameters);
	%>

	<table border="1">
		<tr bordercolor="#FFFFFF">
			<td bgcolor="#CC7777">
				<%
					Date date = new Date();
					Calendar cal = Calendar.getInstance();

					SimpleDateFormat time = new SimpleDateFormat(
							"yyyy年MM月dd日(E) HH:mm:ss");
				%> <%=time.format(date)%>
			</td>
			<td bordercolor="#FFFFFF">
				<form action=android_main.jsp>
					<button type="submit">リセット</button>
				</form>
			</td>
		</tr>
		<tr bordercolor="#FFFFFF">
			<td bgcolor="#CC7777" Align="center">メイン</td>
		</tr>
		<tr bordercolor="#FFFFFF">
			<td bgcolor="#CC7777" Align="center">テーブル情報</td>
		</tr>
		<tr bordercolor="#FFFFFF">
			<td bordercolor="#FFFFFF" Align="center">
				<form action=orderrecord>
					<button type="submit" value="SQL5">配膳状況へ</button>
				</form>
			</td>
		</tr>
	</table>

	<br>



	<form action=tablerecord method="post">
		<table border="1" Align="left">

			<%
				ArrayList<String> metaList = resultList.get(0);
			%>
			<tr>
				<td></td>
				<%
					for (int column = 1; column < resultList.get(0).size(); column++) {
						String metadata = resultList.get(0).get(column);
				%>
				<td><%=metadata%></td>
				<%
					}
				%>
			</tr>
			<%
				for (int row = 1; row < resultList.size(); row++) {
					String id = null;
			%>
			<tr>
				<%
					for (int column = 0; column < resultList.get(row).size(); column++) {
							String data = null;
				%>
				<%
					switch (column) {
							// ラジオボタンの追加
							case 0:
								id = resultList.get(row).get(column);
				%>
				<td><input type="radio" name="tableid" value="<%=id%>"></td>
				<%
					break;

							// 喫煙席情報の置換
							case 4:
								data = resultList.get(row).get(column);
								if ("1".equals(data)) {
									data = smoke[1];
								} else {
									data = smoke[0];
									if (row == 0) {
										data = resultList.get(row).get(column);
									}
								}
				%>
				<td><%=data%></td>
				<%
					break;

							// 着席数のプルダウンリスト
							case 2:
								data = resultList.get(row).get(column);
								System.out.println(resultList.get(row).get(column + 1));
								int max = Integer.parseInt(resultList.get(row).get(
										column + 1));
								int now = Integer.parseInt(resultList.get(row).get(
										column));
				%>
				<td><input type="hidden" name="currentCustomers<%=id%>"
					value="<%=now%>"> <SELECT name="customers<%=id%>">
						<%
							for (int customers = 0; customers <= max; customers++) {
											if (customers == now) {
						%>
						<OPTION value="<%=customers%>" selected><%=customers%></OPTION>
						<%
							} else {
						%>

						<OPTION value="<%=customers%>"><%=customers%></OPTION>
						<%
							}
										}
						%>
				</SELECT></td>
				<%
					break;
							// それ以外の処理
							default:
								data = resultList.get(row).get(column);
				%>
				<td><%=data%></td>
				<%
					break;
							} //switchの終点
						} // forループの終点
				%>
			</tr>
			<%
				} // レコードのforループの終点
			%>
			<tr>
				<td />
				<td>
					<button type="submit" name="sql" value="SQL3">テーブル登録(人数)へ</button>
				</td>
			</tr>
		</table>
	</form>
	<br>

	<%--メニュー情報 --%>

	<%
		// メニュー情報の取得
		parameters.clear();
		parameters.add("SQL8");
		resultList = DBAccess.executeSelect("AN", "wildcard",
				DBAccess.SQL_SELECT_COMMAND, parameters);

		sql = resultList.get(1).get(0);
		parameters.clear();
		System.out.println(parameters.size());
		resultList = DBAccess.executeSelect("AN", "wildcard", sql,
				parameters);
	%>

	<form action=orderrecord method="post">

		<%
			ArrayList<String> labels = resultList.get(0);
			String categoryName = resultList.get(1).get(1);
			String beforeCategoryName = categoryName;

			for (int row = 1; row < resultList.size(); row++) {
		%>
		<table border="1" align="center">
			<tr>
				<th><%=categoryName%></th>
			</tr>

			<%
				while (beforeCategoryName.equals(categoryName)) {
			%>
			<tr>
				<%
					// 用いるデータをチェックし、必要な変数を用意する
							String id = null;
							String bgcolor = "#FFFFFF";

							for (int column = 0; column < resultList.get(row).size(); column++) {
								String data = resultList.get(row).get(column);
								String label = labels.get(column);

								switch (column) {
								case 0:
									id = data;
									break;
								case 4:

									if ("1".equals(resultList.get(row).get(column))) {
										bgcolor = "#ee7777";
									}

									break;
								default:
									break;

								}

							}
							for (int column = 0; column < resultList.get(row).size(); column++) {
								String data = resultList.get(row).get(column);
								String label = labels.get(column);
								switch (column) {
								case 0:
								case 1:
								case 4:
									break;
								default:
				%>
				<td bgcolor="<%=bgcolor%>"><%=data%></td>
				<%
					break;
								} // switchの終点
							} // カラム情報の終点
								// 数量入力を追加する
				%>
				<td><SELECT name="<%=id%>">
						<OPTION value="0">0</OPTION>
						<OPTION value="1">1</OPTION>
						<OPTION value="2">2</OPTION>
						<OPTION value="3">3</OPTION>
						<OPTION value="4">4</OPTION>
						<OPTION value="5">5</OPTION>
						<OPTION value="6">6</OPTION>
						<OPTION value="7">7</OPTION>
						<OPTION value="8">8</OPTION>
						<OPTION value="9">9</OPTION>
				</SELECT></td>
				<%
					// 次の行へ移行する
							row++;
							if (row < resultList.size()) {
								categoryName = resultList.get(row).get(1);
							} else {
								break;
							}
						} // whileのカッコ
				%>
			</tr>

			<tr>
				<td colspan="3">
					<button type="submit">オーダー確認画面へ</button>
				</td>
			</tr>
			<%
				// テーブルタグを閉じる
					row--;
					beforeCategoryName = categoryName;
			%>

		</table>
		<br>
		<%
			}// ループ終点
		%>

	</form>
</body>
</html>